//
//  Add_Manual_ContactViewController.m
//  HuaRun2
//
//  Created by admin on 17/9/7.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Add_Manual_ContactViewController.h"
#import "LabelTextfieldView.h"
#import "CompanyViewController.h"
#import "LineView.h"
#import "LabelImageView.h"
#import "Home_Set_TimeZoneCell.h"
#import "ConditionModel.h"
#import "CompanyModel.h"
#import "Contact_DetailModel.h"
#import "ChooseAddressViewController.h"
#import "ChooseCompanyController1.h"

@interface Add_Manual_ContactViewController ()<UITableViewDelegate, UITableViewDataSource>


@property(nonatomic, strong)UIScrollView * scrollView;

@property(nonatomic, strong)UILabel * chengWeiLab;
//称谓
@property(nonatomic, strong)LabelImageView * chengWeiView;

//姓英文
@property(nonatomic, strong)LabelTextfieldView * firstNameEngLF;
//名英文
@property(nonatomic, strong)LabelTextfieldView * lastNameEngLF;
//姓名（中）
@property(nonatomic, strong)LabelTextfieldView * naemChiLF;

////公司英文
//@property(nonatomic, strong)LabelTextfieldView * companyEngLF;
////公司中文
//@property(nonatomic, strong)LabelTextfieldView * companyChiLF;

//starLabel
@property (nonatomic, strong) UILabel * starLab;
//母公司
@property(nonatomic, strong)UIView * companyMotherView;
@property(nonatomic, strong)UILabel * companyMotherNameLab;

//部门英
@property(nonatomic, strong)LabelTextfieldView * bumenEngLF;
//部门中
@property(nonatomic, strong)LabelTextfieldView * bumenChiLF;
//职位英
@property(nonatomic, strong)LabelTextfieldView * titleEngLF;
//职位中
@property(nonatomic, strong)LabelTextfieldView * titleChiLF;

//电话
@property(nonatomic, strong)LabelTextfieldView * phoneLF;
//传真
@property(nonatomic, strong)LabelTextfieldView * chuanZhenLF;
//香港手机
@property(nonatomic, strong)LabelTextfieldView * hkPhoneLF;
//内地手机
@property(nonatomic, strong)LabelTextfieldView * innerPhoneLF;
//电邮
@property(nonatomic, strong)LabelTextfieldView * emialLF;

//线1
@property (nonatomic, strong) LineView * line1;
//线2
@property (nonatomic, strong) LineView * line2;

@property (nonatomic, strong) UITableView * tableView;
//提示公司 英 不能为空
@property (nonatomic, strong) UILabel * infoLab;

//基本信息
@property (nonatomic, strong) UIView * baseInfoView;
//联系方式
@property (nonatomic, strong) UIView * contactView;
//职位信息
@property (nonatomic, strong) UIView * detailView;

//选择国家视图
@property (nonatomic, strong) UIView * addressBgView;
//国家
@property (nonatomic, strong) UILabel * addressValLab;
//国家
@property (nonatomic, strong) UILabel * addressDesLabel;
@property (nonatomic, strong) UIImageView * iconView;

@end

@implementation Add_Manual_ContactViewController


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    _scrollView.contentSize = CGSizeMake(kMainScreenWidth, CGRectGetMaxY(self.titleChiLF.frame) + kMargin10);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.automaticallyAdjustsScrollViewInsets=NO;
    
    self.scrollView.contentInset=UIEdgeInsetsMake(0, 0, 64, 0);
}

-(void)methodBind {
    
    weakself(ws);
    [self.chengWeiView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws chooseChengWeiMethod];
    }];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        //子线程处理事件如：处理耗时操作、网络请求等
        
        [self fetchConfitionalByType:@"salutation"];
        
    });

    if (_pageStyle == PageStyleEdit) {
//    if (self.isEditStyle) {
        [self configureContactInfo];
    }else if ( _pageStyle == PageStyleScan) {
    
        [self configureContactInfo];
    }else if (_pageStyle ==PageStyleNormal) {
    }
    
    [self.addressBgView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        NSLog(@"...");
        [ws chooseAddress];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewInit {
    [super viewInit];
    
    if (_pageStyle == PageStyleEdit) {
//    if (self.isEditStyle) {
        self.navigationItem.title = NSLocalizedString(@"edit_contact", nil);
    
    }else {
        self.navigationItem.title = NSLocalizedString(@"add_contact", nil);
    }
    
    weakself(ws);
    [self nbLeftImageName:@"qf_back" taget:self clickBlock:^(id result) {
        if (ws.pageStyle == PageStyleEdit) {

            [ws.navigationController popViewControllerAnimated:YES];
        }else {
        
            [ws dismissViewControllerAnimated:YES completion:^{
                
            }];
        }
    }];
    
    [self nbRightImageName:@"save" title:nil taget:self clickBlock:^(id result) {
        [ws submitMethod];
    }];
    
    self.view.backgroundColor = [UIColor whiteColor];// RGBA(249, 249, 249, 1);
    
    [self.view addSubview:self.scrollView];

    [self.scrollView addSubview:self.chengWeiLab];
    [self.scrollView addSubview:self.chengWeiView];
//    [self.scrollView addSubview:self.chengWeiLF];
    [self.scrollView addSubview:self.firstNameEngLF];
    [self.scrollView addSubview:self.lastNameEngLF];
    [self.scrollView addSubview:self.naemChiLF];
    [self.scrollView addSubview:self.line1];
    
//    [self.scrollView addSubview:self.companyEngLF];
//    [self.scrollView addSubview:self.companyChiLF];

    self.companyMotherView = [self makeCompanyMotherView];
    [self.scrollView addSubview:self.companyMotherView];

    [self.scrollView addSubview:self.bumenEngLF];
    [self.scrollView addSubview:self.bumenChiLF];
    [self.scrollView addSubview:self.titleEngLF];
    [self.scrollView addSubview:self.titleChiLF];
    [self.scrollView addSubview:self.line2];

    
    [self.scrollView addSubview:self.phoneLF];
    [self.scrollView addSubview:self.chuanZhenLF];
    [self.scrollView addSubview:self.hkPhoneLF];
    [self.scrollView addSubview:self.innerPhoneLF];
    [self.scrollView addSubview:self.emialLF];
    
    
    [self.scrollView addSubview:self.baseInfoView];
    [self.scrollView addSubview:self.contactView];
    [self.scrollView addSubview:self.detailView];

    [self.scrollView addSubview:self.addressBgView];
    [self.scrollView addSubview:self.addressDesLabel];
    [self.scrollView addSubview:self.addressValLab];
    [self.scrollView addSubview:self.iconView];
    
    [self.scrollView addSubview:self.tableView];
    
    [self.companyMotherNameLab addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        
        [ws chooseMotherCompany];
    }];

}

- (void)layoutInit {
    
    weakself(ws);
    [self.baseInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.scrollView).offset(0);
        make.left.right.mas_equalTo(ws.scrollView);
        make.height.mas_equalTo(40);
    }];

    [self.contactView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.naemChiLF.mas_bottom).offset(0);
        make.left.right.mas_equalTo(ws.scrollView);
        make.height.mas_equalTo(40);
    }];

    [self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.innerPhoneLF.mas_bottom).offset(0);
        make.left.right.mas_equalTo(ws.scrollView);
        make.height.mas_equalTo(40);
    }];

    [self.chengWeiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.baseInfoView.mas_bottom).offset(kMargin10);
        make.right.mas_equalTo(ws.firstNameEngLF.nameLab.mas_right);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(ws.chengWeiView);
    }];

    [self.chengWeiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.chengWeiLab);
        make.left.mas_equalTo(ws.firstNameEngLF.textField);
        make.width.mas_equalTo(80);
    }];
    
    [self.chengWeiView.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.chengWeiView.nameLab.mas_right).offset(kMargin10 / 2);
        make.size.mas_equalTo( CGSizeMake(15, 10));
        make.centerY.mas_equalTo(ws.chengWeiView);
        make.right.mas_equalTo(ws.chengWeiView).offset(-kMargin10 /2);

    }];
    

    [self.firstNameEngLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.line1.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.lastNameEngLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.firstNameEngLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.naemChiLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.lastNameEngLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];

    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.chengWeiLab.mas_bottom).offset(kMargin10);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(ws.scrollView).offset(kMargin10);
        make.right.mas_equalTo(ws.scrollView).offset(-kMargin10);
    }];
    
    
    
//    [self.companyEngLF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(ws.line1.mas_bottom).offset(kMargin10);
//        make.left.right.mas_equalTo(ws.scrollView);
//    }];
    
//    [self.companyChiLF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(ws.companyEngLF.mas_bottom).offset(kMargin10);
//        make.left.right.mas_equalTo(ws.scrollView);
//    }];

    [self.companyMotherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.detailView.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
        make.height.mas_equalTo(ws.naemChiLF);
    }];

    [self.addressDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.companyMotherView.mas_bottom).offset(kMargin10);
        make.left.mas_equalTo(ws.scrollView);
        make.height.mas_equalTo(ws.naemChiLF);
        make.right.mas_equalTo(ws.addressValLab.mas_left).offset(0);
    }];

    [self.addressValLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.companyMotherView.mas_bottom).offset(kMargin10);
        make.right.mas_equalTo(ws.iconView.mas_left).offset(-kMargin10);
        make.height.mas_equalTo(ws.naemChiLF);
        make.width.mas_equalTo(ws.scrollView).multipliedBy(0.6);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.scrollView).offset(-kMargin10);
        make.centerY.mas_equalTo(ws.addressValLab);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    [self.addressBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.addressDesLabel);
        make.top.bottom.mas_equalTo(ws.addressValLab);
        make.right.mas_equalTo(ws.iconView);
    }];

    [self.bumenEngLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.addressDesLabel.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    [self.bumenChiLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.bumenEngLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    [self.titleEngLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.bumenChiLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    [self.titleChiLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.titleEngLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.companyMotherView.mas_bottom).offset(kMargin10);
        make.left.right.height.mas_equalTo(ws.line1);
    }];
    
    [self.emialLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.contactView.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.phoneLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.emialLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.chuanZhenLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.phoneLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.hkPhoneLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.chuanZhenLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];
    
    [self.innerPhoneLF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.hkPhoneLF.mas_bottom).offset(kMargin10);
        make.left.right.mas_equalTo(ws.scrollView);
    }];

    
    CGFloat height = 200;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.chengWeiView.mas_bottom);
        make.left.right.mas_equalTo(ws.chengWeiView);
        make.height.mas_equalTo(height);
    }];
}
#pragma mark  UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Home_Set_TimeZoneCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ConditionModel * conditionModel = _dataArray[indexPath.row];
    cell.valueLab1.text = conditionModel.value;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ConditionModel * conditionModel = _dataArray[indexPath.row];
    _chengWeiView.nameLab.text = [NSString showSpaceStringWithStr: conditionModel.value];
    tableView.hidden = YES;

}
#pragma mark - method

//选择地址
- (void)chooseAddress {
    NSLog(@"---%@", _companyMotherNameLab.text);
    
    if (_companyMotherNameLab.text.length == 0) {
        [self showAlert:NSLocalizedString(@"chooseCompany", nil)];
        return;
    }
    
    weakself(ws);
    ChooseAddressViewController * addressVC = [ChooseAddressViewController new];
    addressVC.clickBlock = ^(NSString *addStr, NSString * name) {
        ws.addStr = addStr;
        ws.addressValLab.text = [NSString stringWithFormat:@"  %@",
                                 name];
    };
    addressVC.contactId = _detailModel.id ? _detailModel.id : @"";
    addressVC.accountId = _accountId ? _accountId : @"";
    addressVC.isFromEditContact = _pageStyle == PageStyleEdit;
    [self.navigationController pushViewController:addressVC animated:YES];
}


- (void)configureContactInfo {
    
    self.chengWeiView.nameLab.text = [NSString showSpaceStringWithStr:_detailModel.salutation];
    _firstNameEngLF.textField.text = [NSString showStringWithStr:_detailModel.lastName];
    _lastNameEngLF.textField.text = [NSString showStringWithStr:_detailModel.firstName];
    _naemChiLF.textField.text =  [NSString showStringWithStr:_detailModel.nameChinese];
    
    //如果是扫描页进来的， 看存在accountId没有，若不存在则不显示公司名称，让用户自己先选择公司
    //    若存在，就显示公司名
    if (_pageStyle == PageStyleScan) {
        BOOL hasAccount = ![NSString isemptyStr:_detailModel.accountId];
        if (hasAccount) {
            _companyMotherNameLab.text = [NSString stringWithFormat:@" %@",
                                          [NSString showStringWithStr:_detailModel.accountName]];
            _accountId = _detailModel.accountId;
        }
        
    }else if (_pageStyle == PageStyleEdit) {
        
        _companyMotherNameLab.text = [NSString stringWithFormat:@" %@",
                                      [NSString showStringWithStr:_detailModel.accountName]];
    }
    
    _bumenEngLF.textField.text = [NSString showStringWithStr:_detailModel.department];
    _bumenChiLF.textField.text = [NSString showStringWithStr:_detailModel.departmentChinese];
    
    _titleEngLF.textField.text = [NSString showStringWithStr:_detailModel.title];
    _titleChiLF.textField.text = [NSString showStringWithStr:_detailModel.titleChinese];
    
    _phoneLF.textField.text = [NSString showStringWithStr:_detailModel.general];
    _chuanZhenLF.textField.text = [NSString showStringWithStr:_detailModel.faxC];
    
    _hkPhoneLF.textField.text = [NSString showStringWithStr:_detailModel.hkMobile];
    _innerPhoneLF.textField.text = [NSString showStringWithStr:_detailModel.chinaMobile];
    
    _emialLF.textField.text = [NSString showStringWithStr:_detailModel.email];
    
}
- (void)changeContactInfo:(NSDictionary *)dic {
    NSMutableDictionary * finalDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    [finalDic setValue:[_detailModel.id stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                forKey:@"id"];
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kEditContactUrl];
    
    weakself(ws);
    [self showActivity:NSLocalizedString(@"正在提交", nil)];
    
    [NetWorkHelper postWithURLString:url parameters:finalDic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        [ws showAlert:kEditSuccess completionBlock:^{
            [ws.navigationController popViewControllerAnimated:YES];
        }];
        
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
}

//选择称谓
- (void)chooseChengWeiMethod {
    _tableView.hidden = !_tableView.hidden;
    
}

//选择母公司
-(void)chooseMotherCompany {
    
    weakself(ws);
    ChooseCompanyController1 * countryVC = [ChooseCompanyController1 new];
    countryVC.clickBlock = ^(id result) {
        CompanyModel * companyModel = (CompanyModel *)result;
        ws.companyMotherNameLab.text = [NSString stringWithFormat:@"  %@",
                                        companyModel.name];
        ws.accountId = companyModel.id;
        ws.infoLab.hidden = YES;
    };
    [self.navigationController pushViewController:countryVC animated:YES];

//    CompanyViewController * companyVC = [CompanyViewController new];
//    companyVC.companyType = CompanyTypeMother;
//    weakself(ws);
//    companyVC.clickBlock = ^(id result) {
//        CompanyModel * companyModel = (CompanyModel *)result;
//        ws.companyMotherNameLab.text = [NSString stringWithFormat:@"  %@",
//                                        companyModel.name];
//        ws.accountId = companyModel.id;
//        ws.infoLab.hidden = YES;
//    };
//    [self.navigationController pushViewController:companyVC animated:YES];
}

- (void)submitMethod {
    [self.view endEditing:YES];
    
    
    BOOL isInputFirstName = [self requireInputfirstNameMethod];
    if (!isInputFirstName) {
        return;
    }
    BOOL isInputCompanyName= [self requireInputCompanyNameMethod];
    if (!isInputCompanyName) {
        return;
    }

    BOOL isEmail = [self requireEmailInputMethod];
    if (!isEmail) {
        return;
    }
    
    NSLog(@"....完成");
    
    //    return;
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kAddContactUrl];
    NSString * firstName = [NSString showStringWithStr:_firstNameEngLF.textField.text];
    NSString * lastName =  [NSString showStringWithStr:_lastNameEngLF.textField.text];
    NSString * nameChinese =  [NSString showStringWithStr:_naemChiLF.textField.text];

    NSString * companyEng = [NSString showStringWithStr:_accountId];
    
    NSString * bumenText = [NSString showStringWithStr: _bumenEngLF.textField.text];
    NSString * bumenChiText = [NSString showStringWithStr: _bumenChiLF.textField.text];
    
    NSString * title = [NSString showStringWithStr:_titleEngLF.textField.text];
    NSString * titleChi = [NSString showStringWithStr:_titleChiLF.textField.text];

    
    NSString * phone = [NSString showStringWithStr:_phoneLF.textField.text];

    NSString * chuanZhen = [NSString showStringWithStr:_chuanZhenLF.textField.text];
    NSString * hkMobile = [NSString showStringWithStr:_hkPhoneLF.textField.text];

    NSString * innerPhone = [NSString showStringWithStr:_innerPhoneLF.textField.text];
    NSString * email = [NSString showStringWithStr:_emialLF.textField.text];
//    [email stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
    NSString * chengWei = [NSString showStringWithStr:_chengWeiView.nameLab.text];
//#电话、内地手机是哪个参数
    NSDictionary * dic = @{@"salutation": [chengWei stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           
                           @"lastName": [firstName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           @"firstName": [lastName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           @"nameChinese": [nameChinese stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           
                           @"accountId":[companyEng stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                               
                           @"department": [bumenText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                            @"departmentChinese":[bumenChiText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           
                           @"title" : [title stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           @"titleChinese": [titleChi stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           
                           @"general":[phone stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           @"faxC":[chuanZhen stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           
                           @"hkMobile":[hkMobile stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           @"chinamobile":[innerPhone stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           
                           @"email":[email stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           @"addressId": _addStr ? _addStr : @""
                           };
    if (_pageStyle == PageStyleEdit) {
        [self changeContactInfo:dic];
        return;
    }
//    return;
    [self showActivity:NSLocalizedString(@"正在提交", nil)];

    weakself(ws);
    [NetWorkHelper postWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            [ws showAlert:object[@"msg"]];
            return;
        }
        [ws showAlert:kAddSuccess completionBlock:^{
            [ws.navigationController dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
    
}

//必填
- (BOOL)requireInputCompanyNameMethod {
    NSString * text = _companyMotherNameLab.text ;
    _infoLab.hidden = text.length >0;
    return text.length > 0;
}

//必填
- (BOOL)requireInputfirstNameMethod {
    NSString * text = _firstNameEngLF.textField.text ;
    if (text.length <= 0) {
        [_firstNameEngLF showInfoStrInLabelIsEmpty:YES                                    text:NSLocalizedString(@"xing_error", nil)];
    }
    _firstNameEngLF.infoLab.hidden = text.length >0;
    
    return text.length > 0;
}
//邮箱输入
- (BOOL)requireEmailInputMethod {
    NSString * text = _emialLF.textField.text ;
    _emialLF.infoLab.hidden = text.length > 0;

    if (text.length <= 0) {

        [_emialLF showInfoStrInLabelIsEmpty:YES
         
                                       text:NSLocalizedString(@"eamil_error",                                                                       nil)];
        return NO;
    }else {
        BOOL isVaildEmail = [text canUseEmail];
        NSLog(@"%@",isVaildEmail ? @"可用": @"不可用邮箱");
        if ( !isVaildEmail) {
            [_emialLF showInfoStrInLabelIsEmpty:NO text:@""];
            return NO;
        }
        return YES;
    }
//    return text.length > 0;
}

//可选输入
- (BOOL)optionalPhoneInputMethod {
    NSString * text = _phoneLF.textField.text ;
    if (text.length > 0) {
        if ( ![text canUsePhoneNum]) {
            [_phoneLF showInfoStrInLabelIsEmpty:NO text:@""];
            return NO;
        }
    }
    _phoneLF.infoLab.hidden = YES;
    return YES;
}

- (void)resetEmailInfoLabelMethod {
    _emialLF.infoLab.hidden = YES;
}

- (void)resetFirstNameInfoLabelMethod {
    _firstNameEngLF.infoLab.hidden = YES;
}

#pragma mark - getter
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}
- (LabelTextfieldView *)firstNameEngLF {
    if (!_firstNameEngLF) {
        self.firstNameEngLF = [LabelTextfieldView new];
        _firstNameEngLF.nameLab.text = NSLocalizedString(@"xing", nil);
//        @"姓(英)";
        _firstNameEngLF.starLab.hidden = NO;
//        [_firstNameEngLF.textField addTarget:self
//                                    action:@selector(requireInputfirstNameMethod)
//                          forControlEvents:UIControlEventEditingChanged];
        [_firstNameEngLF.textField addTarget:self
                                      action:@selector(resetFirstNameInfoLabelMethod )
                            forControlEvents:UIControlEventEditingDidBegin];
    }
    return _firstNameEngLF;
}

- (LabelTextfieldView *)lastNameEngLF {
    if (!_lastNameEngLF) {
        self.lastNameEngLF = [LabelTextfieldView new];
        _lastNameEngLF.nameLab.text = NSLocalizedString(@"name", nil);
        //@"名(英)";
    }
    return _lastNameEngLF;
}

- (LabelTextfieldView *)naemChiLF {
    if (!_naemChiLF) {
        self.naemChiLF = [LabelTextfieldView new];
        _naemChiLF.nameLab.text = NSLocalizedString(@"name_chinese", nil);
        //@"姓名(中)";
    }
    return _naemChiLF;
}


//"department"= "部门(英)";
- (LabelTextfieldView *)bumenEngLF {
    if (!_bumenEngLF) {
        self.bumenEngLF = [LabelTextfieldView new];
        _bumenEngLF.nameLab.text =NSLocalizedString(@"department", nil);
//        @"部门(英)";
    }
    return _bumenEngLF;
}

//"department_chinese"= "部门(中)";
- (LabelTextfieldView *)bumenChiLF {
    if (!_bumenChiLF) {
        self.bumenChiLF = [LabelTextfieldView new];
        _bumenChiLF.nameLab.text = NSLocalizedString(@"department_chinese", nil);
//        @"部门(中)";
    }
    return _bumenChiLF;
}

//"job"= "职位(英)";
- (LabelTextfieldView *)titleEngLF {
    if (!_titleEngLF) {
        self.titleEngLF = [LabelTextfieldView new];
        _titleEngLF.nameLab.text = NSLocalizedString(@"job", nil);
//        @"职位(英)";
    }
    return _titleEngLF;
}

//"job_chinese"= "职位(中)";
- (LabelTextfieldView *)titleChiLF {
    if (!_titleChiLF) {
        self.titleChiLF = [LabelTextfieldView new];
        _titleChiLF.nameLab.text = NSLocalizedString(@"job_chinese", nil);
//        @"职位(中)";
    }
    return _titleChiLF;
}

// "telephone"= "电话";
- (LabelTextfieldView *)phoneLF {
    if (!_phoneLF) {
        self.phoneLF = [LabelTextfieldView new];
        _phoneLF.nameLab.text = NSLocalizedString(@"telephone", nil);
//        @"电话";
        _phoneLF.textField.keyboardType =UIKeyboardTypePhonePad;
//        [_phoneLF.textField addTarget:self
//                               action:@selector(optionalPhoneInputMethod)
//                     forControlEvents:UIControlEventEditingChanged];
        
    }
    return _phoneLF;
}

// "max"= "传真";
- (LabelTextfieldView *)chuanZhenLF {
    if (!_chuanZhenLF) {
        self.chuanZhenLF = [LabelTextfieldView new];
        _chuanZhenLF.nameLab.text =NSLocalizedString(@"max", nil);
        _chuanZhenLF.textField.keyboardType = UIKeyboardTypePhonePad;
        // @"传真";
    }
    return _chuanZhenLF;
}

//"hongkong_phone"= "香港手机";
- (LabelTextfieldView *)hkPhoneLF {
    if (!_hkPhoneLF) {
        self.hkPhoneLF = [LabelTextfieldView new];
        _hkPhoneLF.nameLab.text = NSLocalizedString(@"hongkong_phone", nil);
//        @"香港手机";
        _hkPhoneLF.textField.keyboardType =UIKeyboardTypePhonePad;
//        [_hkPhoneLF.textField addTarget:self
//                               action:@selector(optionalPhoneInputMethod)
//                     forControlEvents:UIControlEventEditingChanged];
        
    }
    return _hkPhoneLF;
}
//"dalu_phone"= "内地手机";
- (LabelTextfieldView *)innerPhoneLF {
    if (!_innerPhoneLF) {
        self.innerPhoneLF = [LabelTextfieldView new];
        _innerPhoneLF.nameLab.text = NSLocalizedString(@"dalu_phone", nil);
//        @"内地手机";
        _innerPhoneLF.textField.keyboardType =UIKeyboardTypePhonePad;
//        [_innerPhoneLF.textField addTarget:self
//                               action:@selector(optionalPhoneInputMethod)
//                     forControlEvents:UIControlEventEditingChanged];
        
    }
    return _innerPhoneLF;
}


//"mail"= "电邮";

- (LabelTextfieldView *)emialLF {
    if (!_emialLF) {
        self.emialLF = [LabelTextfieldView new];
        _emialLF.nameLab.text =NSLocalizedString(@"mail", nil);
        //@"电邮";
        [_emialLF.textField addTarget:self
                               action:@selector(resetEmailInfoLabelMethod )
                     forControlEvents:UIControlEventEditingDidBegin];
        _emialLF.textField.keyboardType =UIKeyboardTypeEmailAddress;
        _emialLF.starLab.hidden = NO;
    }
    return _emialLF;
}

- (LineView *)line1 {
    if (!_line1) {
        self.line1 = [LineView new];
    }
    return _line1;
}

- (LineView *)line2 {
    if (!_line2) {
        self.line2 = [LineView new];
    }
    return _line2;
}

- (LabelImageView *)chengWeiView {
    if (!_chengWeiView) {
        self.chengWeiView = [LabelImageView new];
        _chengWeiView.iconView.image = [UIImage imageNamed:@"save_yanse"];

    }
    return _chengWeiView;
}
//"call"= "称谓";
- (UILabel *)chengWeiLab {
    if (!_chengWeiLab) {
        self.chengWeiLab = [UILabel new];
        _chengWeiLab.textAlignment = NSTextAlignmentRight;
        _chengWeiLab.font = kGeneralFontSize;
        _chengWeiLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _chengWeiLab.text = NSLocalizedString(@"call", nil);
        // @"称谓";
    }
    return _chengWeiLab;
}

- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[Home_Set_TimeZoneCell class]
           forCellReuseIdentifier:@"cell"];
        _tableView.tableHeaderView = [UIView new];
        _tableView.tableFooterView = [UIView new];
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.hidden = YES;
        _tableView.layer.borderColor =  RGBA(195, 202, 205, 1).CGColor;
        _tableView.layer.borderWidth = 1;
        _tableView.layer.masksToBounds = YES;
        _tableView.layer.cornerRadius = 5;
    }
    return _tableView;
}

//获取筛选的条件
- (void)fetchConfitionalByType:(NSString *)type {
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kSelectConditionUrl];
    
    weakself(ws);
    NSDictionary * dic = @{@"type": type};
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            [ws showWithText:[NSString showStringWithStr:object[@"msg"]]];
            return;
        }
        NSArray * array = [NSArray yy_modelArrayWithClass:[ConditionModel class] json:object[@"obj"]];
        
        ws.dataArray = array;
        [ws.tableView reloadData];

    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
}
- (UIView *)makeCompanyMotherView {
    UIView * view = [UIView new];
    [view addSubview:self.starLab];
    
    UILabel * nameLab = [UILabel new];
    nameLab.textAlignment = NSTextAlignmentRight;
//    nameLab.backgroundColor = [UIColor yellowColor];
    nameLab.font = kGeneralFontSize;
    nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    nameLab.text = NSLocalizedString(@"company", nil);
//    @"公司(英)";
    [view addSubview:nameLab];
    
    UILabel * infoLab = [UILabel new];
    infoLab.textAlignment = NSTextAlignmentLeft;
    infoLab.font = kGeneralFontSize;
    infoLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
//    infoLab.backgroundColor = [UIColor yellowColor];
//    infoLab.layer.borderColor = RGBA(231, 233, 234, 1).CGColor;
//    infoLab.layer.borderWidth = 1;
//    infoLab.layer.masksToBounds = YES;
//    infoLab.layer.cornerRadius = 10;
    [view addSubview:infoLab];
    
    UIImageView * iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:@"return-拷贝"];
    [view addSubview:iconView];
    
    [view addSubview:self.infoLab];
    
    [self.starLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(nameLab.mas_left).offset(2);
        make.height.centerY.mas_equalTo(nameLab);
    }];

    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.mas_equalTo(view);
        make.right.mas_equalTo(infoLab.mas_left).offset(0);
    }];
    
    [infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(iconView.mas_left).offset(-kMargin10);
        make.centerY.mas_equalTo(view);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(view).multipliedBy(0.6);
    }];

    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(view).offset(-kMargin10);
        make.centerY.mas_equalTo(view);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(10);
    }];
    
    [self.infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(infoLab);
        make.height.mas_equalTo(10);
        make.top.mas_equalTo(infoLab.mas_bottom).offset(0);
    }];

    
    self.companyMotherNameLab = infoLab;
    
    return view;
}
- (UILabel *)starLab {
    if (!_starLab) {
        self.starLab = [UILabel new];
        _starLab.textAlignment = NSTextAlignmentRight;
        _starLab.font = kFontSize15;
        _starLab.textColor = [UIColor orangeColor];
        _starLab.text = @"*";
    }
    return _starLab;
}
- (UILabel *)infoLab {
    if (!_infoLab) {
        self.infoLab = [UILabel new];
        _infoLab.textAlignment = NSTextAlignmentLeft;
        _infoLab.font = kGeneralFontSize;
        _infoLab.textColor = [UIColor redColor];
        
        _infoLab.text = NSLocalizedString(@"contact_company_error", nil);
        
//        [NSString stringWithFormat:@"公司(英)不能为空，请选择"];
        _infoLab.hidden = YES;
    }
    return _infoLab;
}

- (UIView *)makeCommonHeaderViewWithTitle:(NSString *)title
                                imageName:(NSString *)imageName {
    UIView * view = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kMainScreenWidth, 40))];
    view.backgroundColor = RGBA(249, 249, 249, 1);
    
    UIImageView * imageView = [UIImageView new];
    imageView.frame = CGRectMake(10, 12.5, 15, 15);
    imageView.image = [UIImage imageNamed:imageName];
    [view addSubview:imageView];
    
    UILabel * label = [UILabel new];
    label.frame = CGRectMake(30, 10, 100, 20);
    label.textAlignment = NSTextAlignmentLeft;
    label.font = kGeneralFontSize;
    label.text = title;
    [view addSubview:label];
    
    return view;
}

- (UIView *)baseInfoView {
    if (!_baseInfoView) {
        self.baseInfoView = [self makeCommonHeaderViewWithTitle:NSLocalizedString( @"基本信息", nil)
                                                      imageName:@"基本信息"];
    }
    return _baseInfoView;
}

- (UIView *)contactView {
    if (!_contactView) {
        self.contactView = [self makeCommonHeaderViewWithTitle:@"联系方式"
                                                     imageName:@"联系方式"];
    }
    return _contactView;
}

- (UIView *)detailView {
    if (!_detailView) {
        self.detailView = [self makeCommonHeaderViewWithTitle:@"职位信息"
                                                    imageName:@"职位"];
    }
    return _detailView;
}


- (UILabel *)addressDesLabel {
    if (!_addressDesLabel) {
        self.addressDesLabel = [UILabel new];
        _addressDesLabel.textAlignment = NSTextAlignmentRight;
        _addressDesLabel.font = kGeneralFontSize;
        _addressDesLabel.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _addressDesLabel.text = NSLocalizedString(@"companyAddress", nil);
        _addressDesLabel.backgroundColor = kRandomColor;
        _addressDesLabel.numberOfLines = 0;
    }
    return _addressDesLabel;
}

- (UILabel *)addressValLab {
    if (!_addressValLab) {
        self.addressValLab = [UILabel new];
        _addressValLab.textAlignment = NSTextAlignmentLeft;
        _addressValLab.font = kGeneralFontSize;
        _addressValLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
//        _addressValLab.text = @"value";
        _addressValLab.backgroundColor = kRandomColor;
        _addressValLab.numberOfLines = 0;
        
    }
    return _addressValLab;
}

- (UIView *)addressBgView {
    if (!_addressBgView) {
        self.addressBgView = [UIView new];
        _addressBgView.backgroundColor = [UIColor clearColor];
    }
    return _addressBgView;
}

- (UIImageView *)iconView {
    if (!_iconView) {
        self.iconView = [UIImageView new];
        _iconView.image = [UIImage imageNamed:@"return-拷贝"];
    }
    return _iconView;
}

@end

